package com.itheima.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * 演示: 操作es客户端的创建
 *
 * 创建索引库:  setting   mapping
 *
 * @作者 itcast
 * @创建日期 2021/3/19 9:09
 **/
public class EsDemo01 {
    RestHighLevelClient client;
    @Test
    public void createIndex() throws IOException {
        //1 . 创建一个请求对象 ( CreateIndexRequest )
        CreateIndexRequest request = new CreateIndexRequest("user");// 索引库名称
        // 1.1 setting
        request.settings("{\n" +
                "    \"number_of_shards\": 1,\n" +
                "    \"number_of_replicas\": 1\n" +
                "  }",XContentType.JSON);
        // 1.2 mapping   参数1: 映射的json字符串   参数2: 字符串的格式类型
        request.mapping("{\n" +
                "    \"properties\": {\n" +
                "      \"id\": {\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"name\":{\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"age\":{\n" +
                "        \"type\": \"integer\"\n" +
                "      },\n" +
                "      \"gender\":{\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"note\":{\n" +
                "        \"type\": \"text\",\n" +
                "        \"analyzer\": \"ik_max_word\"\n" +
                "      }\n" +
                "    }\n" +
                "  }", XContentType.JSON); //代表字符串是json
        //2.  调用client执行创建索引库的方法 () client.indices().create()  indices:代表要对索引库进行操作  create:创建索引库
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        //3.  解析响应结果
        System.out.println("创建结果: " + response.isAcknowledged());
    }

    /**
     * 判断索引库是否存在
     * @throws IOException
     */
    @Test
    public void existIndex() throws IOException {
        // 1. 创建对应的请求对象
        GetIndexRequest request = new GetIndexRequest("user");
        // 2. 执行对应的请求方法
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        // 3. 解析响应结果
        System.out.println(exists);
    }
    @Test
    public void deleteIndex() throws IOException {
        // 1. 创建对应的请求对象
        DeleteIndexRequest request = new DeleteIndexRequest("user");
        // 2. 执行对应的请求方法
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        // 3. 解析响应结果
        System.out.println(response.isAcknowledged());
    }

    @Before
    public void init(){
        // 创建操作es的客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.200.129",9200,"http")
                )
        );
    }
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        // 创建操作es的客户端
//        RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost("192.168.200.129",9200,"http")
//                )
//        );
//        // 使用client操作es
//
//        // 使用完毕后需要释放链接
//        try {
//            client.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


}
